package cn.dev.mybatis.core;

import cn.dev.mybatis.cache.SqlCache;
import cn.dev.mybatis.core.SingleFactory;
import cn.dev.mybatis.proxy.ProxyFactory;

import java.sql.Connection;

public class SqlSession implements AutoCloseable {
    private final Connection connection;

    private final boolean autoCommit;

    public SqlSession(Connection connection, boolean autoCommit) {
        this.connection = connection;
        this.autoCommit = autoCommit;
    }

    public <T> T getMapper(Class<T> clazz) {
        return getMapper(clazz, autoCommit);
    }

    public <T> T getMapper(Class<T> clazz, boolean autoCommit) {
        return ProxyFactory.getProxyObject(clazz, connection, autoCommit);
    }

    @Override
    public void close() {
        SingleFactory.get(SqlCache.class).clear();
    }
}
